package skill;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.entity.Player;

import pluginMain.Configer;
import pluginMain.SP;
import skill.SkillBase;

public class SkillPlayer {
	private Player owner;
	private List<Skill> Skills = new ArrayList<Skill>();
	private Configer playerCfg;

	public SkillPlayer(List<SkillBase> skills, Player _target) {
		owner = _target;
		playerCfg = new Configer(SP.instance, SP.PlayerDataFolder, owner.getName() + ".yml");
		for (SkillBase b : skills) {
			// create skill instance
			Skills.add(new Skill(owner, b, playerCfg.getInt(b.getSkillName() + ".Level")));
		}
		
	}

	public Player getOwner() {
		return owner;
	}

	public List<Skill> getAllSkill() {
		return Skills;
	}

	public List<Skill> getSkillByType(SkillBase.SkillType type) {
		List<Skill> skills = new ArrayList<Skill>();
		for (Skill s : Skills)
			if (s.getSkillBase().getSkillType().equals(type))
				skills.add(s);

		return skills;
	}

	public boolean learnNextLevel(String skillName) {
		if (getSkillByName(skillName).equals(null))
			return false;

		if (owner.getTotalExperience() >= getSkillByName(skillName).getNextLevelExp()) {
			int oldExp = owner.getTotalExperience();
			oldExp-=getSkillByName(skillName).getNextLevelExp();
			owner.setTotalExperience(oldExp);
			getSkillByName(skillName).setNextLevel();
			playerCfg.set(skillName + ".Level", getSkillByName(skillName).getLevel());
			owner.sendMessage("你的" + skillName + "技能等级提升为" + getSkillByName(skillName).getLevel());
			savePlayerData();
			SP.instanceSB.showInfo(owner, "&2经验值", oldExp);
			SP.instanceSB.showInfo(owner, SP.SCORE_HLH, (int) owner.getMaxHealth());
			return true;
		} else {
			owner.sendMessage("你的经验不足！下一级需要经验:" + getSkillByName(skillName).getNextLevelExp());
			return false;
		}
		
	}

	public boolean learnNextLevelAdmin(String skillName, Player player) {
		getSkillByName(skillName).setNextLevel();
		playerCfg.set(skillName + ".Level", getSkillByName(skillName).getLevel());
		player.sendMessage("你的" + skillName + "技能等级提升为" + getSkillByName(skillName).getLevel());
		savePlayerData();
		return true;
	}

	public Skill getSkillByName(String name) {
		for (Skill s : Skills) {
			if (s.getSkillBase().getSkillName().equals(name)) {
				return s;
			}
		}

		return null;
	}

	public void savePlayerData() {
		playerCfg.saveConfig();
	}

	public void reloadPlayerData() {
		playerCfg.forceReload();
	}

}
